package org.reliablesource.prism.gef.editor.commands;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.reliablesource.prism.core.IPrismAnnotation;
import org.reliablesource.prism.gef.editor.model.ScreenAdapter;
import org.reliablesource.prism.gef.editor.model.StickyNoteAdapter;
import org.reliablesource.prism.gef.editor.parts.ScreenEditPart;


/**
 *
 *  StickyNoteSetConstraintCommand.java
 *  Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 *
 *	Created on: Apr 11, 2006 
 *  @author Dennis Park 
 *  	<a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 */

public class StickyNoteSetConstraintCommand extends Command{
	/** Stores the new size and location. */
	private final Rectangle newBounds;
	/** Stores the old size and location. */
	private Rectangle oldBounds;
	/** A request to move/resize an edit part. */
	private final ChangeBoundsRequest request;

	/** Shape to manipulate. */
	private final StickyNoteAdapter stickNote;

	private final ScreenEditPart parent;
	/**
	 * Create a command that can resize and/or move a stickyNote. 
	 * @param stickyNote	the stickyNote to manipulate
	 * @param req		the move and resize request
	 * @param newBounds the new size and location
	 * @throws IllegalArgumentException if any of the parameters is null
	 */
	public StickyNoteSetConstraintCommand(StickyNoteAdapter _textBox, ChangeBoundsRequest _req, Rectangle _newBounds, EditPart _parent) {
		if (_textBox == null || _req == null || _newBounds == null || _parent == null) {
			throw new IllegalArgumentException();
		}
		this.stickNote = _textBox;
		this.request = _req;
		this.newBounds = _newBounds.getCopy();
		this.parent = (ScreenEditPart) _parent;
		setLabel("move / resize");
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	public boolean canExecute() {
		Object type = request.getType();
		// make sure the Request is of a type we support:
		return (RequestConstants.REQ_MOVE.equals(type)
				|| RequestConstants.REQ_MOVE_CHILDREN.equals(type) 
				|| RequestConstants.REQ_RESIZE.equals(type)
				|| RequestConstants.REQ_RESIZE_CHILDREN.equals(type));
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		oldBounds = new Rectangle(stickNote.getLocation(), stickNote.getSize());
		redo();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	public void redo() {
		stickNote.setSize(newBounds.getSize());
		stickNote.setLocation(newBounds.getLocation());
		((ScreenAdapter)parent.getModel()).childPropertyChange(stickNote, IPrismAnnotation.LOCATION, newBounds);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		stickNote.setSize(oldBounds.getSize());
		stickNote.setLocation(oldBounds.getLocation());
		((ScreenAdapter)parent.getModel()).childPropertyChange(stickNote, IPrismAnnotation.LOCATION, oldBounds);
	}
}
